import { defineStore } from 'pinia'
import useMapDataStore from './mapData'
import { cloneDeep, shuffle } from 'lodash-es'
import { message } from 'ant-design-vue'

interface GameDataState {
  mainAreaCards: number[][][] // 主区域卡片
  removeAreaCards: number[][] // 移除区域卡片
  cacheAreaCards: number[] // 缓存区域卡片
  remainCards: number[] // 剩余卡片(不包括移除区域卡片与缓存区域卡片)
}

const useGameDataStore = defineStore('gameData', {
  state: () => {
    return {
      mainAreaCards: [],
      removeAreaCards: [],
      cacheAreaCards: [],
      remainCards: []
    } as GameDataState
  },
  actions: {
    initGame() {
      const {
        currentMap: { mainArea, cardsCount, cardsTypesCount }
      } = useMapDataStore()
      // 重置移除区域卡片与缓存区域卡片
      this.removeAreaCards = []
      this.cacheAreaCards = []
      // 重置剩余卡片数组
      const eachTotal = cardsCount / cardsTypesCount
      const cards: number[] = []
      for (let i = 0; i < cardsTypesCount; i++) {
        for (let j = 0; j < eachTotal; j++) {
          cards.push(i + 1)
        }
      }
      this.remainCards = shuffle(cards)
      // 将地图克隆到游戏中
      this.mainAreaCards = cloneDeep(mainArea)
      // 重置游戏中主区域的卡片
      this.resetArea()
    },
    shuffleMap() {
      this.remainCards = shuffle(this.remainCards)
      this.resetArea()
    },
    resetArea() {
      const { mainAreaCards, remainCards } = this
      let remainIndex = 0
      // 循环遍历主区域中的每个有卡片的位置，依次对其重新赋值
      for (let i = 0; i < mainAreaCards.length; i++) {
        const row = mainAreaCards[i]
        for (let j = 0; j < row.length; j++) {
          const col = row[j]
          for (let k = 0; k < col.length; k++) {
            if (mainAreaCards[i][j][k] > 0) {
              mainAreaCards[i][j][k] = remainCards[remainIndex++]
            }
          }
        }
      }
    },
    moveCard(type: number) {
      const { cacheAreaCards } = this
      const obj: any = {}
      // 检查缓存区域中的卡片
      for (let i = 0; i < cacheAreaCards.length; i++) {
        if (cacheAreaCards[i] === type) {
          const typeStr = `${type}`
          if (!obj[typeStr]) {
            obj[typeStr] = true
          } else {
            // 如果缓存区域中已有2个与移动卡片相同的卡片，则消除这2张卡片
            this.cacheAreaCards = cacheAreaCards.filter((item) => item !== type)
            return
          }
        }
      }
      // 将移动卡片放入缓存区域
      cacheAreaCards.push(type)
      // 缓存区域同时存在7张卡片无法消除时则游戏结束
      if (cacheAreaCards.length === 7) {
        message.error('游戏失败!')
        this.initGame()
      }
    },
    moveMainAreaCard(type: number, level: number, row: number, col: number) {
      const { mainAreaCards, remainCards } = this
      // 从主区域消除移动卡片
      mainAreaCards[level][row][col] = 0
      const index = remainCards.findIndex((item) => item === type)
      remainCards.splice(index, 1)
      this.moveCard(type)
    },
    moveRemoveAreaCard(type: number, level: number, index: number) {
      const { removeAreaCards } = this
      // 从移除区域消除移动卡片
      removeAreaCards[level][index] = 0
      this.moveCard(type)
    },
    removeCacheAreaCard() {
      const { removeAreaCards, cacheAreaCards } = this
      removeAreaCards.push(cacheAreaCards.slice(0, 3))
      cacheAreaCards.splice(0, 3)
    }
  }
})

export default useGameDataStore
